import { useEventEmitter, useMount } from 'ahooks'
import { lazy, Suspense, useState } from 'react'
import { GetIndexedDB } from '../Utils/IndexedDB'
import { themeStore, pageStore, userPowerStore, modelStore, drawerStore, fromStore } from '../Config/zustand'


const LoadSvg = lazy(() => import('../View/Loading/LoadSvg'))

const index = () => {

    const { updateModel } = modelStore()
    const { updateFromData } = fromStore()
    const { updateDrawer } = drawerStore()

    const { updateTheme, updateLogo, updateMenu, updateHead, updateBody } = themeStore()
    const { updateActive, updateList } = pageStore()
    const { updateUserPower } = userPowerStore()

    const [Theme, setTheme] = useState()

    useMount(async () => {
        updateModel({})
        updateFromData({})
        updateDrawer({})
        const key = await GetIndexedDB('key')
        const config = await GetIndexedDB('config')
        const token = await GetIndexedDB('token')
        const user = await GetIndexedDB('user')
        const openKey = await GetIndexedDB('openKey')
        const pageList = await GetIndexedDB('pageList')
        const menuList = await GetIndexedDB('menu')
        const powerList = await GetIndexedDB('power')
        const { theme, logo, menu, head, body } = config
        updateTheme(theme)
        updateLogo(logo)
        updateMenu(menu)
        updateHead(head)
        updateBody(body)
        updateActive(openKey)
        updateList(pageList)
        const { color, template } = theme
        const ThemeTemp = lazy(() => import(`./theme/${template}.jsx`))
        window.AUTH = '123'
        window.API = '223'
        window.IMG = '323'
        setTheme(ThemeTemp)
        updateUserPower(powerList)
    })

    const AdminEvent = useEventEmitter()
    AdminEvent.useSubscription(val => {
        console.log('触发顶层回调', val)
    })


    if (Theme) {
        return (
            <Suspense>
                {Theme ?
                    <Theme AdminEvent={AdminEvent} /> :
                    <LoadSvg />
                }
            </Suspense>
        )
    }



};

export default index;